package com.hujiang.basic.search.handler;

import com.hujiang.basic.framework.plugin.mq.MqPlugin;
import com.hujiang.basic.framework.plugin.mq.consume.Consumer;
import com.hujiang.basic.framework.rest.common.BaseService;
import com.hujiang.basic.search.handler.impl.ForwardingHandler;
import com.hujiang.basic.search.handler.impl.model.HandlerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

@Service
public class HandlerService extends BaseService<HandlerInfo> {

    @Autowired
    private ForwardingHandler forwarder;

    private static final String NAMESPACE = "handlerInfo";
    public static Set<String> listeningQueues = new CopyOnWriteArraySet<>();

    public void listen(String queue) {
        Consumer consumer = new Consumer();
        consumer.setQueue(queue);
        consumer.setHandler(forwarder);
        MqPlugin.listen(consumer);
    }

    public void stopListen(String queue) {
        MqPlugin.stopListen(queue);
    }

    public void pause(String queue) {
        update(NAMESPACE, "pause", queue);
    }

    public void restart(String queue) {
        update(NAMESPACE, "restart", queue);
    }

    //新索引上线先往db写入queue避免二次上线
    @Override
    public int insert(HandlerInfo queueInfo) {
        return insert(NAMESPACE, "insert", queueInfo);
    }


    @Override
    protected String namespace() {
        return NAMESPACE;
    }


}
